package com.yu.queue;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockingQueueDemo {

    static BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);

    public static void main(String[] args) {
        new Thread(()->{
            while (true){
                try {
                    Thread.sleep(100);
                    queue.put(String.valueOf(new Random().nextInt(5)));
                    System.out.println("生产者在生产，目前有：" + queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"Producer").start();

        new Thread(()->{
            while (true){
                try {
                    Thread.sleep(120);
                    queue.take();
                    System.out.println("消费者在消费，目前剩余：" + queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"consumer").start();
    }


}
